package cn.sgjk.train.business.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.sgjk.train.business.domain.DailyTrainCarriage;
import cn.sgjk.train.business.domain.TrainCarriage;
import cn.sgjk.train.business.enums.SeatColEnum;
import cn.sgjk.train.business.mapper.DailyTrainCarriageMapper;
import cn.sgjk.train.business.mapper.TrainCarriageMapper;
import cn.sgjk.train.business.req.TrainCarriageSaveReq;
import cn.sgjk.train.common.context.LoginMemberContext;
import cn.sgjk.train.common.exception.BusinessException;
import cn.sgjk.train.common.resp.CommonResp;
import cn.sgjk.train.common.resp.PageResp;
import cn.sgjk.train.business.req.DailyTrainCarriageQueryReq;
import cn.sgjk.train.business.req.DailyTrainCarriageSaveReq;
import cn.sgjk.train.business.resp.DailyTrainCarriageQueryResp;
import cn.sgjk.train.business.service.DailyTrainCarriageService;
import cn.sgjk.train.common.util.SnowUtil;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.sgjk.train.common.exception.BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR;

@RestController
@RequestMapping("/admin/daily-train-carriage")
public class DailyTrainCarriageAdminController {

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainCarriageAdminController.class);
    @Autowired
    private DailyTrainCarriageService dailyTrainCarriageService;
    @Autowired
    private TrainCarriageMapper mapper;


    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody DailyTrainCarriageSaveReq dailyTrainCarriageReq) {
        dailyTrainCarriageService.save(dailyTrainCarriageReq);
        return new CommonResp<>();
    }



    @GetMapping("/query-list")
    public CommonResp<PageResp<DailyTrainCarriageQueryResp>> query(@Valid DailyTrainCarriageQueryReq dailyTrainCarriageQueryReq) {
        PageResp<DailyTrainCarriageQueryResp> dailyTrainCarriageQueryResps = dailyTrainCarriageService.queryList(dailyTrainCarriageQueryReq);
        return new CommonResp<>(dailyTrainCarriageQueryResps);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        dailyTrainCarriageService.delete(id);
        return new CommonResp<>();
    }


    //方便手动输入车厢


    @GetMapping("/genCarriage")
    public CommonResp<Object> genCaariage() {
        TrainCarriage dailyTrainCarriageReq = new  TrainCarriage();
        dailyTrainCarriageReq.setTrainCode("G1");
        for (int i = 1; i <= 18; i++) {
            DateTime now = DateTime.now();
            dailyTrainCarriageReq.setCreateTime(now);
            dailyTrainCarriageReq.setUpdateTime(now);
            if(i == 1 || i == 5 || i == 8) {
                dailyTrainCarriageReq.setSeatType("1");
                dailyTrainCarriageReq.setSeatCount(52);
                dailyTrainCarriageReq.setRowCount(13);
                dailyTrainCarriageReq.setColCount(4);
                dailyTrainCarriageReq.setId(SnowUtil.getSnowflakeNextId());
                dailyTrainCarriageReq.setIndex(i);
                mapper.insert(dailyTrainCarriageReq);
                continue;
            }
           /* dailyTrainCarriageReq.setSeatType("2");
            dailyTrainCarriageReq.setSeatCount(85);
            dailyTrainCarriageReq.setRowCount(17);
            dailyTrainCarriageReq.setColCount(5);
            dailyTrainCarriageReq.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriageReq.setIndex(i);
            mapper.insert(dailyTrainCarriageReq);*/

        }
         return new CommonResp<>();
    }


}

